// my/wx.js
import {
  InvioceModel
} from '../../../models/invioce'
import {
  PayModel
} from '../../../models/pay'
import {
  Format
} from '../../../utils/common'
import {
  AccountModel
} from '../../../models/account'
const accountmodel = new AccountModel()
const payModel = new PayModel()
const invioceModel = new InvioceModel()
const app = getApp()
let last
const format = new Format()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoiceLists: '', //可开票列表
    openTicketType: '', //开票类型
    invoiceUnbilledLogIds: [],  //选中开票的order
    modalOpened4: false //
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  onShow: function () {
    console.log('1')
    this.data.invoiceUnbilledLogIds = []
    this.findOrderReceiptListNew()
    // this.loadMyAccount()
    // if (app.globalData.ifSetPaw == 0 ) {
    //   this.setData({
    //     modalOpened4: true,
    //   });
    // } else if (app.globalData.ifSetPaw == 1) {
    // } else {
    //   this.checkOriginPwd()
    // }
  },
  // 获取可开票列表
  findOrderReceiptListNew() {
    invioceModel.findOrderReceiptListNew(0,1000).then(res => {
      console.log(res)
      if (res && res.code == '00') {
        this.setData({
          'invoiceLists': res.invoices
        })
        if (res.openTicketType) {
          this.setData({
            'openTicketType': res.openTicketType
          })
        }
      } else {
        if (res && res.msg) {
          wx.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          wx.showToast({
            type: 'none',
            content: '查询发票抬头失败',
            duration: 1000,
          });
        }
        
      }
      
    })
  },
  // 变更开票类型
  checkout(e) {
    const index = e.currentTarget.dataset.index
    if (index == this.data.openTicketType) {
      return
    } else {
      this.modifyOpenTicketType(index)
    }
  },
  // 变更开票类型
  modifyOpenTicketType(index) {
    invioceModel.modifyOpenTicketType(index).then(res => {
      console.log(res)
      if (res && res.code == '00') {
        this.setData({
          'openTicketType': index
        })
      } else {
        if (res && res.msg) {
          wx.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          wx.showToast({
            type: 'none',
            content: '变更开票类型失败',
            duration: 1000,
          });
        }
        
      }
      
    })
  },
  // 合并开票
  toSelect(e) {
    const index = e.currentTarget.dataset.index
    let item = e.currentTarget.dataset.item
    if (!item.isSelect) {
      this.data.invoiceUnbilledLogIds.push(item.orderNum)
      this.getBillAmountByTrades('select',index,item.orderNum)
    } else {
      this.data.invoiceLists.list[index].isSelect = false
      this.setData({
        invoiceLists: this.data.invoiceLists
      })
      for (var i = 0; i < this.data.invoiceUnbilledLogIds.length; i++) {
        if (this.data.invoiceUnbilledLogIds[i] == item.orderNum) {
            this.data.invoiceUnbilledLogIds.splice(i, 1);
        }
      }
    }
  },
  // 获取合并开票金额
  getBillAmountByTrades(type,index,orderNum) {
    invioceModel.getBillAmountByTrades(this.data.invoiceUnbilledLogIds).then(res => {
      console.log(res)
      if (res && res.code == '00') {
        if (type == 'select') {
          this.data.invoiceLists.list[index].isSelect = true
          this.setData({
            invoiceLists: this.data.invoiceLists
          })
        } else {
          wx.navigateTo({
            url: '/pages/invioce/invoiceApplicationConfirm/invoiceApplicationConfirm?unBillAmount=' + res.unBillAmount + '&invoiceUnbilledLogIds=' + this.data.invoiceUnbilledLogIds
          })
        }
      } else {
        if (orderNum) {
          for (var i = 0; i < this.data.invoiceUnbilledLogIds.length; i++) {
            if (this.data.invoiceUnbilledLogIds[i] == orderNum) {
                this.data.invoiceUnbilledLogIds.splice(i, 1);
            }
          }
        }
        if (res && res.msg) {
          wx.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          wx.showToast({
            type: 'none',
            content: '变更开票类型失败',
            duration: 1000,
          });
        }
      }
    })
  },
  toOpen(e) {
    if (this.data.invoiceLists.totalUnbilledAmount == 0) {
      wx.showToast({
        type: 'none',
        content: '您的可开票金额为0',
        duration: 1000,
      });
      return
    }
    if (this.data.invoiceUnbilledLogIds.length > 0) {
      this.getBillAmountByTrades('next',0)
    } else {
      wx.showToast({
        type: 'none',
        content: '请选择开票订单',
        duration: 1000,
      });
    }

  },
  checkOriginPwd() {
    accountmodel.checkOriginPwd().then(res => {
        console.log(res)
        if (res && res.code == '00') {
          if (res.pinStatus == 0) {
            this.setData({
              modalOpened4: true,
            });
            app.globalData.ifSetPaw = 0
          } else {
            app.globalData.ifSetPaw = 1
          }
        } else {
          wx.showToast({
            type: 'none',
            content: '接口错误',
            duration: 1000,
          });
        }
      })
  },
  onModalClick21() {
    this.setData({
      modalOpened4: false,
    });
    wx.navigateTo({
      url: '/pages/account/setPaw/setPaw'
    })
  },
  onModalClose21() {
    this.setData({
      modalOpened4: false,
    });
    wx.navigateBack()
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function () {
  //   // this.loadMyAccount()
  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})